package applicationFunction.fileSystem;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

/**
 * 对比两个文件夹内的所有文件，将相同的文件复制到一个文件夹内
 */
public class CompareDir {
    private static Logger LOGGER = Logger.getLogger(CompareDir.class.getName());

    public static void main(String[] args) {
        String path1 = "C:\\Users\\ShawnLuo\\Desktop\\esb\\esbServer_16";
        String path2 = "C:\\Users\\ShawnLuo\\Desktop\\esb\\esb";
        String outPath = "C:\\Users\\ShawnLuo\\Desktop\\esb\\output";

        Map<String, String> map1 = new HashMap<>();//文件夹1下面的所有文件  key相对路径，String 放是否被使用信息
        Map<String, String> map2 = new HashMap<>();
        getAbsFile(path1, path1, map1);
        getAbsFile(path2, path2, map2);

        Set<String> commonFiles = new HashSet<>();//公共的文件

        getCommonFiles(map1, map2, commonFiles);

        //将 path2 里面的公用文件复制到新的文件夹下面
        copyFile(path2, outPath, commonFiles);


        //        打印各种信息
        boolean flag = true;
        if (flag) {
            System.out.println(path1 + " unused files ");
            for (String key : map1.keySet()) {
                if (map1.get(key).contains("used")) {
                    continue;
                }
                System.out.println(key + "  " + map1.get(key));
            }

            System.out.println();
            System.out.println(path2 + " unused files ");
            for (String key : map2.keySet()) {
                if (map2.get(key).contains("used")) {
                    continue;
                }
                System.out.println(key + "  " + map2.get(key));
            }

            //使用多次的文件
            System.out.println();
            System.out.println("----------------------------");
            System.out.println();
            System.out.println(path1 + " mutil files ");
            for (String key : map1.keySet()) {
                if (map1.get(key).contains("mutil")) {
                    System.out.println(key + "  " + map1.get(key));
                }

            }

            System.out.println();
            System.out.println(path2 + " mutil files ");
            for (String key : map2.keySet()) {
                if (map2.get(key).contains("mutil")) {
                    System.out.println(key + "  " + map2.get(key));
                }
            }
        }

    }


    /**
     * 将某一个文件夹下面的公告文件复制到 新的目录下面
     *
     * @param orginDir
     * @param destDir
     * @param common
     */
    public static void copyFile(String orginDir, String destDir, Set<String> common) {
        File orgin = new File(orginDir);
        if (!orgin.exists()) {
            LOGGER.severe("源目录不存在");
        }
        File dest = new File(destDir);
        if (!dest.exists()) {
            dest.mkdirs();
        }
        for (String filePath : common) {
            File orginFile = new File(orginDir + filePath);
            File destFile = new File(destDir + filePath);
            if (!destFile.getParentFile().exists()) {
                destFile.getParentFile().mkdirs();
            }
            try {
                Files.copy(orginFile.toPath(), destFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
            }
            catch (IOException e) {
                LOGGER.severe(orginFile.toString() + " copy fail");
                e.printStackTrace();
            }

        }
    }

    /**
     * 获取两个文件夹中相同的文件相对路径（相对于path1/path2）
     *
     * @param map1
     * @param map2
     * @param common
     */
    public static void getCommonFiles(Map<String, String> map1, Map<String, String> map2, Set<String> common) {
        for (String key : map1.keySet()) {
            String value1 = map1.get(key);
            String value2 = map2.get(key);
            if (value2 != null && value2.contains("exist")) {
                common.add(key);
                if (value1.contains("used")) {
                    map1.put(key, value1 + "#mutil");
                }
                else {
                    map1.put(key, value1 + "#used");
                }
                if (value2.contains("used")) {
                    map2.put(key, value2 + "#mutil");
                }
                else {
                    map2.put(key, value2 + "#used");
                }
            }
        }
    }

    /**
     * 获取文件相对路径（相对于path1/path2） 名称的hash
     *
     * @param prefixPath
     * @param filePath
     * @param map
     */
    public static void getAbsFile(String prefixPath, String filePath, Map<String, String> map) {
        File file = new File(filePath);
        if (file.exists()) {
            if (file.isDirectory()) {
                for (String tmp : file.list()) {
                    getAbsFile(prefixPath, filePath + File.separator + tmp, map);
                }
            }
            else {
//                System.out.println(file.getAbsolutePath());
                map.put(file.getAbsolutePath().replace(prefixPath, ""), "exist");
            }
        }
    }


}
